﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for ShoppingCart
/// </summary>
public class ShoppingCart:DataTable
{
    public string tb = "";
    public string _Re = "";
	public ShoppingCart()
	{
        this.Columns.Add("ID", typeof(string));
        this.PrimaryKey = new DataColumn[] { this.Columns[0] };
        this.Columns.Add("Sanpham", typeof(string));
        this.Columns.Add("Gia", typeof(double));
        this.Columns.Add("Soluong", typeof(int));
        this.Columns.Add("Hinhanh", typeof(string));
        this.Columns.Add("Tongtien", typeof(float), "Soluong*Gia");
	}
    public string Re
    {
        get
        {
            return _Re;
        }
        set
        {
            this._Re = value;
        }
    }
    public void AddProduct(string ID, string Sanpham, int soluong, double gia,string hinhanh)
    {
        if (ReturnID(ID) == false)
        {
            DataRow r = this.NewRow();
            r["ID"] = ID;
            r["Sanpham"] = Sanpham;
            r["Soluong"] = soluong;
            r["Gia"] = gia;
            r["Tongtien"] = soluong * gia;
            r["Hinhanh"] = hinhanh;
            this.Rows.Add(r);
            tb = "Add Success " + Sanpham;
        }
        else
        {
            tb = "Had Product " + Sanpham;
        }
    }
    public void Update(string ID, double soluong)
    {
        if (ReturnID(ID) == true)
        {
            DataRow objDR = this.Rows.Find(ID);
            DataRow objDR2;
            objDR2 = this.NewRow();
            objDR2["ID"] = ID;
            objDR2["Sanpham"] = objDR["Sanpham"];
            objDR2["Gia"] = objDR["Gia"];
            objDR2["Hinhanh"] = objDR["Hinhanh"];
            objDR2["Soluong"] = soluong;
            objDR2["Tongtien"] = Convert.ToSingle((Convert.ToSingle(objDR["Gia"]) * double.Parse(objDR["Soluong"].ToString())));
            this.Rows.Remove(objDR);
            this.Rows.Add(objDR2);
            tb = "Update Suceessfully Produce(s)";
        }
        else
        {
            tb = "Update NO Suceessfully Produce(s)";
        }

    }

    public void Delete(string ID)
    {
        if (ReturnID(ID) == true)
        {
            DataRow objDR = this.Rows.Find(ID);
            this.Rows.Remove(objDR);
            tb = "Delete Suceessfully Produce(s)";
        }
        else
        {
            tb = "Delete NO Suceessfully Produce(s)";
        }
    }
    public int Laysoluong(string ID, int sluong)
    {
        string sl = "";
        for (int i = 0; i < this.Rows.Count; i++)
        {
            if (this.Rows[i][0].ToString() == ID)
            {
                sl= this.Rows[i][3].ToString();
            }
        }
        return int.Parse(sl)+sluong;
    }

    public bool ReturnID(string ID)
    {
        DataRow r = this.Rows.Find(ID);
        try
        {
            if (r.IsNull("ID"))
                return false;
            else return true;
        }
        catch (Exception) { };
        return false;
    }
    public float Quantity
    {
        get
        {
            object value = this.Compute("SUM(Tongtien)", "");
            
            if (value.Equals(DBNull.Value) == true)
                return 0;
            else
                return float.Parse(value.ToString());
        }
    }
    public string Hinhanh
    {
        get
        {
            object value = this.Hinhanh;
            if (value.Equals(DBNull.Value) == true)
                return "../Img/avartar.jpeg";
            else
                return Convert.ToString(value);
        }
    }

    public int Count
    {
        get { return this.Rows.Count; }
    }
    public string HasProduce
    {
        get { return tb; }
    }
    public double Sum
    {
        get
        {
            double s = 0;
            for (int i = 1; i <= this.Rows.Count; i++)
            {
                s += double.Parse(this.Rows[0][5].ToString());
            }
            return s;
        }
    }
    
}
